package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.ToastUtil;

public class SearchTransFlowerActivity extends BaseActivity {

    private Context mContext;
    private EditText searchEdit;
    private String keyword;
    private View quxiao;
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            from = intent.getIntExtra("From", 0);
        }
        quxiao = findViewById(R.id.quxiao);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        searchEdit = (EditText) findViewById(R.id.search_edit);
        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyword = searchEdit.getText().toString();
                    if(checkInput()) {
                        if (from == 1){
                            Intent jianzheng_intent = new Intent(mContext,SearchTransFlowerListActivity.class);
                            jianzheng_intent.putExtra("keyword",keyword);
                            startActivityForResult(jianzheng_intent,111);
                        }else {
                            Intent intent = new Intent(mContext, SearchChiBanListActivity.class);
                            intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_SEARCH);
                            intent.putExtra("keyword", keyword);

                            startActivity(intent);
                            finish();
                        }
                    }
                }
                return true;
            }
        });
    }

    private boolean checkInput(){
        if(keyword == null || (keyword.trim()).equals("")){
            ToastUtil.showMessage(mContext, R.string.search_no_null);
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ( requestCode == 111 && data != null){
            setResult(RESULT_OK,data);
            finish();
        }
    }

}
